package com.lt.flashcard.view;

import java.util.Date;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Textbox;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.Repository;
import com.lt.flashcard.model.service.UserService;

public class ResetPasswordCtrl extends SelectorComposer<Component> {

    private static final long serialVersionUID = 1L;

    private String code = "";

    @Wire
    Textbox password;
    @Wire
    Textbox repassword;

    private User user;

    private static final Logger log = Logger.getLogger(ResetPasswordCtrl.class);

    @Override
    public ComponentInfo doBeforeCompose(Page page, Component parent, ComponentInfo compInfo) {
        code = Executions.getCurrent().getParameter("code");// Activation code
        if (Utility.isEmptyString(code)) {
            Utility.redirect("index.zul", false);
            return null;
        }

        UserService usrv = Repository.getInstance().getUserService();
        user = usrv.findByActivationCode(code);
        if (user == null) {

            if (log.isInfoEnabled()) {
                log.info("Reset password code not exist: " + code);
            }

            Clients.evalJavaScript("showNotice('', 'Mã không tồn tại')");
            return null;
        }

        if (user.getExpiredate().before(new Date())) {

            if (log.isInfoEnabled()) {
                log.info("Reset password code expired: " + code);
            }

            Clients.evalJavaScript("showNotice('', 'Mã đã hết hạn')");
            user = null;
            return null;
        }

        return super.doBeforeCompose(page, parent, compInfo);
    }

    @Listen("onClick=#btnSave")
    public void btnSave() {
        if (!password.getValue().equals(repassword.getValue())) {
            Clients.evalJavaScript("showNotice('', 'Mật khẩu không giống nhau')");
            return;
        }

        AAService asrv = Repository.getInstance().getAAService();
        UserService usrv = Repository.getInstance().getUserService();
        user.setPassword(asrv.hashPassword(password.getValue()));
        user.setActivation("");
        user.setExpiredate(null);
        usrv.save(user);

        if (log.isInfoEnabled()) {
            log.info("Change pass of " + user.getUsername() + " to: " + password.getValue());
        }

        if (!asrv.doLogin(user.getUsername(), password.getValue(), false, true)) {

            if (log.isInfoEnabled()) {
                log.error("Can't login user: " + user.getUsername() + " pass: " + password.getValue());
            }

            Clients.evalJavaScript("showNotice('', 'Không thể đặt lại password. Vui lòng liên hệ admin')");
        }
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}
